#coding:utf-8

# applogger.py 日志相关逻辑
# Author: Angelic47

import threading
import datetime
import sys

class AppLogger():
	def __init__(self, outToStdout, logFile):
		self.outToStdout = outToStdout
		self.fileHandler = open(logFile, 'a')
		self.mutexLock = threading.Lock()
	def log(self, logStr):
		self.mutexLock.acquire()
		try:
			if(self.outToStdout):
				sys.stdout.write(logStr)
			self.fileHandler.write(logStr)
		except:
			pass
		self.mutexLock.release()
	def logline(self, logStr):
		self.log(logStr + '\n')
	def logPrefix(self, logPrefix, logStr):
		logStr = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' [' + logPrefix + '] ' + logStr
		self.log(logStr)
	def logPrefixln(self, logPrefix, logStr):
		logStr = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' [' + logPrefix + '] ' + logStr
		self.logline(logStr)
